package com.aischool.webapp.teacher;

import com.aischool.common.model.AiTeacher;
import com.aischool.common.model.SysUser;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiClassService;
import com.aischool.service.AiPostService;
import com.aischool.service.AiTeacherService;
import com.aischool.service.FileUploadService;
import com.aischool.service.SysUserService;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/front/teacher/user")
public class AiUserController extends BaseTeacherController {
	
	@Inject
	AiClassService aiClassService;
	@Inject
	AiPostService aiPostService;
	@Inject
	FileUploadService fileUploadService;
	@Inject
	SysUserService sysUserService;
	@Inject
	AiTeacherService aiTeacherService;

	public void getTeacher() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		Object obj = aiPostService.getPostAll();
		jr.setDicKV("post_rd", obj);
		jr.setDicKV("login_user_rd", this.getAttr("login_user_record"));
		jr.setDicKV("login_teacher_rd", this.getAttr("login_teacher_rd"));
		
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void getClasses() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		Record rd = this.getAttr("login_teacher_rd");
		jr.setDicKV("rd_teacher", rd);
		jr.setDicKV("list_classes", aiClassService.getAll(rd.getInt("org_id"), 1));
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
		
	}
	

	public void upload_userhead() {
		
		this.setAttr("filemodel", "base64");
		
		this.setAttr("zoom_type", "wh");
		this.setAttr("zoom_w", 100);
		this.setAttr("zoom_h", 100);
		
		JsonResult jr = fileUploadService.uploadFile(this);
		if (jr.getCode() == 0) {
			int login_user_id = this.getAttrForInt("login_user_id");
			SysUser info = new SysUser();
			info.setUserImg(jr.getDicKey("fileurl"));
			info.setUserId(login_user_id);
			info.setUpdateTime(DateUtil.getTimestamp());
			info.removeNullValueAttrs();
			boolean bf = info.update();
			if (bf) {
				sysUserService.removeCache(login_user_id);
			}else {
				String msg = jr.getMsg();
				jr.setEnum(JsonCodeEnum.Faild);
				jr.appendMsg(msg);
			}
		}
		renderJson(jr.Dic);
	}
	
	public void update_info() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		int login_user_id = this.getAttrForInt("login_user_id");
		int login_teacher_id = this.getAttrForInt("login_teacher_id");
		
		Record rdt = getAttr("login_teacher_rd");
				
		AiTeacher infot =  getModel(AiTeacher.class, "", true);
		infot.setTeacherId(login_teacher_id);
		infot.setUpdateTime(DateUtil.getTimestamp());
		infot.removeNullValueAttrs();
		boolean bf = infot.update();
		if (bf) {
			if (!rdt.get("teacher_name", "").equals(infot.getTeacherName())) {
				SysUser info = new SysUser();
				info.setUserId(login_user_id);
				info.setUpdateTime(DateUtil.getTimestamp());
				info.setUserName(infot.getTeacherName());
				info.removeNullValueAttrs();
				info.update();
				sysUserService.removeCache(login_user_id);
			}
			aiTeacherService.removeCacheByUserId(login_user_id);
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			String msg = jr.getMsg();
			jr.setEnum(JsonCodeEnum.Faild);
			jr.appendMsg(msg);
		}
		
		renderJson(jr.Dic);
	}
	
	public void checkAuth() {
		// 结果类
		JsonResult jr = new JsonResult();
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
}






